package com.mvvm.ktnet.daoUtil

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.github.yuweiguocn.library.greendao.MigrationHelper
import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener
import com.mvvm.ktnet.dao.DaoMaster
import com.mvvm.ktnet.dao.TestDaoDao
import org.greenrobot.greendao.database.Database

class AppOpenHelper @JvmOverloads constructor(
    context: Context?,
    name: String?,
    factory: SQLiteDatabase.CursorFactory?
) :
    DaoMaster.OpenHelper(context, name, factory) {


    override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) {
//        super.onUpgrade(db, oldVersion, newVersion)
        MigrationHelper.migrate(
            db,
            object : ReCreateAllTableListener {
                override fun onCreateAllTables(db: Database, ifNotExists: Boolean) {
                    DaoMaster.createAllTables(db, ifNotExists)
                }

                override fun onDropAllTables(db: Database, ifExists: Boolean) {
                    DaoMaster.dropAllTables(db, ifExists)
                }
            },
            TestDaoDao::class.java,
        )
    }

}